<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    // 冒泡排序
    function bubbleSort(arr) {
        for (let i = 0; i < arr.length; i++) {
            for (let j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    let temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }

    // 生成 1-20之间的随机数
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    const arr = [];
    const min = 1;
    const max = 20
    for (let i = 0; i < 10; i++) {
        arr.push(randomNum(min, max));

    }
    console.log('排序前：', arr)

    console.log('排序后', bubbleSort(arr));

    const arr1 = [12, 23, 34, 12, 23, 34];

    // 数组去重
    function unique(arr) {
        const newArr = [];
        for (let i = 0; i < arr.length; i++) {
            if (newArr.indexOf(arr[i]) === -1) {
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }

    console.log('unique', unique(arr1));

    // 创建一个 jsonp 函数
    function jsonp(url, data, callback) {
        const callbackName = 'jsonp_' + Date.now();
        window[callbackName] = function (data) {
            callback(data);
            // 删除该函数
            delete window[callbackName];
        }
        const queryString = Object.keys(data).map(key => {
            return `${key}=${data[key]}`
        }).join('&');
        const script = document.createElement('script');
        script.src = url + '?' + queryString + '&callback=' + callbackName;
        document.body.appendChild(script);
    }

    let json_url = 'https://s.pcmgr.qq.com/tapi/web/softlistcgi.php?callback=loadList&c=0&offset=56&limit=28&noplugin=0';
    jsonp(json_url, {
        name: 'jsonp'
    }, function (data) {
        console.log(data);
    });


    // 生成年月日，时分秒
    function getDate() {
        const date = new Date();
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();
        const second = date.getSeconds();
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }

    console.log(getDate());

    // 邮箱验证正则
    function isEmail(str) {
        const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        return reg.test(str);
    }

    // 手机号正则验证
    function isPhone(str) {
        const reg = /^1[3-9]\d{9}$/;
        return reg.test(str);
    }

    //身份证验证
    function isIdCard(str) {
        const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        return reg.test(str);
    }

    var iphone = 18620627898;
    var email = 'gogdas@163.com';
    var cardNumber = 232131123123123123;
    console.log(isPhone(iphone), isEmail(email), isIdCard(cardNumber));

    // 在 vscode 里面有一个插件，按照该插件后就可以是通过编码注释的方式去生成代码

    // 快速排序
    function quickSort(arr) {
        if (arr.length <= 1) {
            return arr;
        }
        const pivotIndex = Math.floor(arr.length / 2);
        const pivot = arr.splice(pivotIndex, 1)[0];
        const left = [];
        const right = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] < pivot) {
                left.push(arr[i]);
            } else {
                right.push(arr[i]);
            }
        }
        return quickSort(left).concat([pivot], quickSort(right));
    }



</script>

</html>